package com.itheima.mp.Service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mp.Service.UserService;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Autowired
private UserMapper userMapper;
    public void deducte(Long id, Integer money) {
        //1.查询用户
        User user = getById(id);
        //2.检验用户状态
        Integer status = user.getStatus();
        if (user ==null || status==2){
            throw new RuntimeException("用户状态异常");
        }
        //3.检验余额是否充足
        if (user.getBalance()<money){
            throw new RuntimeException("用户余额不足");
        }
        //4.扣减余额
       baseMapper.deducteMoney(id,money);

    }

    @Override
    public List<User> queryUsers(String name, Integer status, Integer minBalance, Integer maxBalance) {

        return lambdaQuery()
                .like(name!=null,User::getUsername,name)
                .eq(status!=null,User::getStatus,status)
                .ge(minBalance!=null,User::getBalance,minBalance)
                .le(maxBalance!=null,User::getBalance,maxBalance)
                .list();
    }
}
